﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;
using StoreClientModule;

namespace MBBStoreClient
{
    public partial class frmPurchaseOrderManager : Form
    {
        UnityProcessor proc = new UnityProcessor();
        Threader t = new Threader();
        UnityScript[] dat = new UnityScript[0];
        string filter = "";
        public frmPurchaseOrderManager()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            txtFilter.SelectedIndex = 0;
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);
            t.MethodCompleted += new EventHandler(t_MethodCompleted);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            grdItems.DoubleClick += new EventHandler(grdItems_DoubleClick);
            txtSearch.KeyPress += new KeyPressEventHandler(txtSearch_KeyPress);
            txtSearch.Focus();
        }

        void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnSearch_Click(null, null);
            }
        }

      
        void grdItems_DoubleClick(object sender, EventArgs e)
        {
            Common.parent.ViewPO(grdItems.Rows[grdItems.SelectedRows[0].Index].Cells[0].Value.ToString());            
        }
        void btnSearch_Click(object sender, EventArgs e)
        {
            filter = txtFilter.Text;
            t.MethodCall(Search);
        }
        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {

            });
        }
        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "SEARCH":
                        proc.DBPUX_Data_Fetch(out dat);
                        grdItems.DataSource = dat[0].ToDataTable();
                        lblStatus.Text = dat[0].Data_Count + " Results(s) @ " + proc.ProcEnd() + " sec.";
                        FormatGrid();
                        break;
                }
            });
        }
        void t_MethodCompleted(object sender, EventArgs e)
        {
            t.SafeCall(this, delegate
            {
                
            });
        }
        //Operations
        void Search()
        {
            while (proc.IsRunning)
            {

            }
            proc.ProcStart();
            string src = txtSearch.Text;
            UnityScript u = new UnityScript("SEARCH", 5);

            u.Script_Add(Script.DBPUX_BEGIN);
            switch (filter)
            {
                case "All":
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, t1.fNumber AS 'P.O. Number', ffName AS 'Supplier', fDate AS 'Date', ffPrice AS 'Price', ffPay AS 'Items', fTerms AS 'Terms', fStamp " +
                        "FROM tblpurchaseorder t1,(SELECT fName AS ffName, fRefNum FROM tblsupplierprofile) AS t2, (SELECT SUM(fQty * fPrice) AS ffPrice, t33.fNumber, COUNT(*) AS ffPay FROM tblpurchaseorderitems t33 GROUP BY t33.fNumber) AS t3 " +
                        "WHERE ((t1.fRefNum=t2.fRefNum)AND(t1.fNumber=t3.fNumber)) AND (t1.fNumber LIKE '%{0}%' OR ffName LIKE '%{0}%' OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%') " +
                        "GROUP BY t1.fNumber", src));
                    break;
                case "With Payments":
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, t1.fNumber AS 'P.O. Number', ffName AS 'Supplier', fDate AS 'Date', ffPrice AS 'Price', ffPay AS 'Payments Amount', (ffPrice - ffPay) AS Balance, fStamp " +
                        "FROM tblpurchaseorder t1,(SELECT fName AS ffName, fRefNum FROM tblsupplierprofile) AS t2, (SELECT SUM(fQty * fPrice) AS ffPrice, t33.fNumber FROM tblpurchaseorderitems t33 GROUP BY t33.fNumber) AS t3, (SELECT SUM(fPaymentAmount) AS ffPay, t44.fNumber  FROM tblpurchaseorderpayments t44 GROUP BY t44.fNumber) AS t4 " +
                        "WHERE ((t1.fRefNum=t2.fRefNum)AND(t1.fNumber=t3.fNumber)AND(t1.fNumber=t4.fNumber)) AND (t1.fNumber LIKE '%{0}%' OR ffName LIKE '%{0}%' OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%') " +
                        "GROUP BY t1.fNumber", src));
                    break;
                case "Without Payments":
                    
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, t1.fNumber AS 'P.O. Number', ffName AS 'Supplier', fDate AS 'Date', ffPrice AS 'Price', ffPay AS 'Items', fTerms AS 'Terms', fStamp " +
                       "FROM tblpurchaseorder t1,(SELECT fName AS ffName, fRefNum FROM tblsupplierprofile) AS t2, (SELECT SUM(fQty * fPrice) AS ffPrice, t33.fNumber, COUNT(*) AS ffPay FROM tblpurchaseorderitems t33 GROUP BY t33.fNumber) AS t3 " +
                       "WHERE ((t1.fRefNum=t2.fRefNum)AND(t1.fNumber=t3.fNumber)AND(t1.fNumber NOT IN (SELECT t4.fNumber FROM tblpurchaseorderpayments t4))) AND (t1.fNumber LIKE '%{0}%' OR ffName LIKE '%{0}%' OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%') " +
                       "GROUP BY t1.fNumber", src));
                    break;
            }

            u.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(u);
        }
        void Search2()
        {
            while (proc.IsRunning)
            {

            }
            proc.ProcStart();
            string src = txtSearch.Text;
            UnityScript u = new UnityScript("SEARCH", 5);
            
            u.Script_Add(Script.DBPUX_BEGIN);
            u.Script_Add(Script.DBPUX_GENERIC, "DROP TEMPORARY TABLE IF EXISTS tmpsupplierprofile");
            u.Script_Add(Script.DBPUX_GENERIC, "CREATE TEMPORARY TABLE tmpsupplierprofile AS (SELECT * FROM tblsupplierprofile)");
            switch (filter)
            {
                case "All":
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fNumber AS 'P.O. Number', fRefNum AS 'Reference Number', fDate AS 'Date', fTerms AS 'Terms',  fStamp FROM tblpurchaseorder WHERE " +
                        "fNumber LIKE '%{0}%' OR fRefNum IN (SELECT fRefNum FROM tmpsupplierprofile WHERE fName LIKE '%{0}%') " +
                        "OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%' AND (fNumber IN (SELECT fNumber FROM tblpurchaseorderpayments)) ORDER BY fRefNum ASC, fNumber DESC", src));
                    break;
                case "With Payments":
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fNumber AS 'P.O. Number', fRefNum AS 'Reference Number', fDate AS 'Date', fTerms AS 'Terms', fStamp  FROM tblpurchaseorder WHERE " +
                        "(fNumber LIKE '%{0}%' OR fRefNum IN (SELECT fRefNum FROM tmpsupplierprofile WHERE fName LIKE '%{0}%') " +
                        "OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%') AND (fNumber IN (SELECT fNumber FROM tblpurchaseorderpayments)) ORDER BY fRefNum DESC, fNumber DESC", src));
                    break;
                case "Without Payments":
                    u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fNumber AS 'P.O. Number', fRefNum AS 'Reference Number', fDate AS 'Date', fTerms AS 'Terms', fStamp  FROM tblpurchaseorder WHERE " +
                        "(fNumber LIKE '%{0}%' OR fRefNum IN (SELECT fRefNum FROM tmpsupplierprofile WHERE fName LIKE '%{0}%') " +
                        "OR fDate LIKE '%{0}%' OR fTerms LIKE '%{0}%') AND (fNumber NOT IN (SELECT fNumber FROM tblpurchaseorderpayments)) ORDER BY fRefNum DESC, fNumber DESC", src));
                    break;
            }

            u.Script_Add(Script.DBPUX_QUERY, "SELECT * FROM tmpsupplierprofile");
            u.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(u);
        }
        void FormatGrid()
        {

            grdItems.Columns[0].Width = (grdItems.Width * 15) / 100;
            grdItems.Columns[1].Width = (grdItems.Width * 25) / 100;
            grdItems.Columns[2].Width = (grdItems.Width * 15) / 100;
            grdItems.Columns[3].Width = (grdItems.Width * 13) / 100;
            grdItems.Columns[4].Width = (grdItems.Width * 13) / 100;
            grdItems.Columns[5].Width = (grdItems.Width * 13) / 100;

            grdItems.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdItems.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdItems.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdItems.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdItems.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            if (grdItems.Rows.Count > 0)
            {
                for (int i = 0; i < grdItems.Rows.Count; i++)
                {
                    grdItems.Rows[i].Cells[2].Value = grdItems.Rows[i].Cells[2].Value.ToString().Split(' ')[0];
                    grdItems.Rows[i].Cells[3].Value = float.Parse(grdItems.Rows[i].Cells[3].Value.ToString()).ToString("#,#0.00");
                    grdItems.Rows[i].Cells[4].Value = float.Parse(grdItems.Rows[i].Cells[4].Value.ToString()).ToString("#,#0.00");
                    grdItems.Rows[i].Cells[5].Value = float.Parse(grdItems.Rows[i].Cells[5].Value.ToString()).ToString("#,#0.00");
                    
                }
            }
        }
      
    }
}
